package com.example.batbat.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.LoginCtrlCom;
import com.example.common.component.UserCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.Description;
import com.example.common.model.MyResponse;
import com.example.common.model.UserApiGetUserFavorResp;
import com.example.common.user.GlobalUser;

public class SplashViewModel extends ViewModel {
    private static final String TAG = "SplashViewModel";
    private LoginCtrlCom loginCtrlCom;
    private UserCtrlCom userCtrlCom;

    private MutableLiveData<Integer> isLoadDone=new MutableLiveData<>();

    public SplashViewModel(){
        loginCtrlCom=new LoginCtrlCom();
        userCtrlCom=new UserCtrlCom();
        isLoadDone.setValue(0);
    }

    /**
     * 验证是否登录
     */
    public void isLogin(){
        loginCtrlCom.getSession(GlobalUser.TOKEN, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                //设置登录信息
                GlobalUser.IS_LOGIN= (Boolean)response;
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }

            @Override
            public void onFailed(String mes) {
                // todo
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }
        });
    }

    /**
     * 获取头像和用户名
     */
    public void getAvatar(){
        userCtrlCom.getUser(GlobalUser.TOKEN, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                Description resp=(Description)response;
                GlobalUser.AVATAR_URL=resp.getAvatar();
                GlobalUser.USERNAME = resp.getUsername();
                GlobalUser.ID=resp.getId();
                //todo 解决头像不能同步的问题
                GlobalUser.getInstance().getBindAvatarUrl().setValue(resp.getAvatar());
                GlobalUser.getInstance().getBindUserName().setValue(resp.getUsername());
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }

            @Override
            public void onFailed(String mes) {
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }
        });
    }

    /**
     * 设置用户喜爱游戏
     */
    public void setFavorMap(){
        Log.i(TAG, "setFavorMap: "+GlobalUser.ID);
        userCtrlCom.getUserIdFavor(GlobalUser.TOKEN, GlobalUser.ID, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                UserApiGetUserFavorResp resp=(UserApiGetUserFavorResp)response;
                resp.getList().forEach(v->{
                    GlobalUser.getInstance().addFavorMap(v.getGame().getId(),true);
                });
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }

            @Override
            public void onFailed(String mes) {
                Log.i(TAG, "onFailed: "+mes);
                isLoadDone.setValue(isLoadDone.getValue()+1);
            }
        });
    }

    public MutableLiveData<Integer> getIsLoadDone() {
        return isLoadDone;
    }
}
